//
//  OSCCommentView.m
//  OSChina
//
//  Created by 8b410-2 on 17/1/5.
//  Copyright © 2017年 abb. All rights reserved.
//

#import "OSCCommentView.h"

typedef void(^StringBlock)(NSString *);
typedef void(^AttributeBlock)(NSAttributedString *);

@interface OSCCommentView () <UITextViewDelegate>

@property (nonatomic, strong)  UITextView *textView;

@property (nonatomic,strong) UILabel *placeholderLabel;

@property (nonatomic,assign) BOOL isCompleteMax;

@property (nonatomic, copy) StringBlock stringBlock;

@property (nonatomic, copy) AttributeBlock attributeBlock;

@end

@implementation OSCCommentView

- (instancetype)initWithPlaceholder:(NSString *)placeholder WithFont:(UIFont *)font {
    self = [super initWithFrame:CGRectZero];
    if (self) {
        _placeholder = placeholder;
        [self setupUIWithFont:font];
    }
    return self;
}

- (void)setupUIWithFont:(UIFont *)font {
    
    _textView = [UITextView new];
    [_textView setTextContainerInset:UIEdgeInsetsMake(5, 0, 5, 0)];
    _textView.delegate = self;
    _textView.layer.borderColor = [[UIColor colorWithHex:0xc7c7cc] CGColor];
    _textView.layer.borderWidth = 1;
    _textView.layer.cornerRadius = 4;
    _textView.scrollEnabled = NO;
    _textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
    _textView.textContainer.maximumNumberOfLines = 1;
    [self addSubview:_textView];
    
    _placeholderLabel = [[UILabel alloc] init];
    _placeholderLabel.textColor = [UIColor colorWithHex:0xc8c8ce];
    _placeholderLabel.text = @"发表评论";
    _placeholderLabel.font = font;
    [self addSubview:_placeholderLabel];
    
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor colorWithHex:0xc7c7cc];
    [self addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).mas_offset(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
    [_placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.textView).mas_offset(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
}

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    _placeholderLabel.text = _placeholder;
}

- (void)handleAttributeWithString:(NSString *)textString{
    if (textString.length == 0 || textString == nil) {
        self.textView.attributedText = nil;
        _placeholderLabel.hidden = NO;
    }else{
        _placeholderLabel.hidden = YES;
        NSString *targetString = [NSString stringWithFormat:@"[草稿]%@",textString];
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:targetString];
        NSRange firstRange = NSMakeRange(0, 4);
        [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:firstRange];
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0, targetString.length)];
        self.textView.attributedText = attributeString;
    }
}

- (void)handleAttributeWithAttribute:(NSAttributedString *)attribute{
    if (attribute.length != 0) {
        _placeholderLabel.hidden = YES;
        NSString *targetString = [NSString stringWithFormat:@"[草稿]"];
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:targetString];
        NSRange firstRange = NSMakeRange(0, 4);
        [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:firstRange];
        [attributeString appendAttributedString:attribute];
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0, attributeString.length)];
        self.textView.attributedText = attributeString;
    }else{
        self.textView.attributedText = nil;
        _placeholderLabel.hidden = NO;
    }
}

- (void)clickTextViewWithStringBlock:(void (^)(NSString *))block {
    _stringBlock = block;
}

- (void)clickTextViewWithAttributeBlock:(void (^)(NSAttributedString *))block {
    _attributeBlock = block;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if (_stringBlock) {
        NSString *textString;
        if (self.textView.text.length>0) {
            textString = [self.textView.text substringWithRange:NSMakeRange(4, textView.text.length - 4)];
        }
        _stringBlock(textString);
    }
    if (_attributeBlock) {
        NSAttributedString *attibute;
        if (textView.attributedText.length > 0) {
            attibute = [textView.attributedText attributedSubstringFromRange:NSMakeRange(4, textView.attributedText.length - 4)];
        }
        _attributeBlock(attibute);
    }
    return NO;
}

@end
